<template>
  <div>

  </div>
</template>

<script>
export default {
  data() {
    return {

    }
  },
  created() {
    var _that = this
    //前端面试题：如何求两个数组的并集、交集、差集
    // const arr1 = [33, 22, 22, 55, 33, 11, 33, 5]
    // const arr2 = [22, 22, 55, 77, 88, 88, 99, 99]
    const arr1 = [1, 2, 3]
    const arr2 = [1, 2, 4]

    //并集
    const union = [...new Set([...arr1, ...arr2])]
    console.log("并集", union) //[33, 22, 55, 11,5, 77, 88, 99]

    //交集
    const cross = [...new Set(arr1.filter(item => { return arr2.includes(item) }))]
    console.log("交集", cross) //[ 22, 55 ]

    //差集 arr1-arr2的差集(A-B)
    const diff1 = [...new Set(arr1.filter(item => !cross.includes(item)))]
    console.log("差集A-B", diff1) //[ 33, 11, 5 ]

    //差集 arr2-arr1的差集(B-A)
    const diff2 = [...new Set(arr2.filter(item => !cross.includes(item)))]
    console.log("差集B-A", diff2) //[ 77, 88, 99 ]

    // c = a.filter(item =>{
    //   b.forEach(e => {
    //     console.log(item);
    //     console.log(e);
    //     return item == e;
    //   })
    // })
    // console.log(c);


    // this.getUserIP(function (ip) {
    //   console.log("得到的本地IP :" + ip)  // 得到的本地IP :192.168.21.23
    //   // console.log(_that.loginForm)
    //   // _that.loginForm.userIP = ip  //这里不能用this 因为this指向的是这个回调函数的this
    // })

  },
  mounted() {

  },
  methods: {
    // 获取ip地址，这个方法并不好用，需要浏览器授权获取ip
    getUserIP(onNewIP) {
      console.log('获取ip')
      let MyPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection
      let pc = new MyPeerConnection({ iceServers: [] })
      let noop = function () { }
      let localIPs = {}
      let ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g
      function iterateIP(ip) {
        if (!localIPs[ip]) onNewIP(ip)
        localIPs[ip] = true
      }
      pc.createDataChannel('')
      pc.createOffer().then(function (sdp) {
        sdp.sdp.split('\n').forEach(function (line) {
          if (line.indexOf('candidate') < 0) return
          line.match(ipRegex).forEach(iterateIP)
        })
        pc.setLocalDescription(sdp, noop, noop)
      }).catch(function (reason) {
        // An error occurred, so handle the failure to connect
      })
      // seen for candidate events
      pc.onicecandidate = function (ice) {
        if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return
        ice.candidate.candidate.match(ipRegex).forEach(iterateIP)
      }
    },


  },
}
</script>

<style scoped>
</style>